package com.one.common.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.one.common.R;
import com.one.common.utils.StringUtils;


/**
 * creator: zhulunjun
 * time:    2018/7/9
 * describe: 输入控件组合
 */

public class InputLayout extends LinearLayout {

    private TextView tvName, tvRequired, tvCenter, tvRight;
    private EditText clearEditView;
    private ImageView ivRight, ivLeft;
    private boolean isEdit;
    private LinearLayout llInput;

    public InputLayout(Context context) {
        super(context, null);
    }

    public InputLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }
        LayoutInflater.from(context).inflate(R.layout.layout_input, this);
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.InputLayout);
        String name = ta.getString(R.styleable.InputLayout_name);
        int nameRes = ta.getResourceId(R.styleable.InputLayout_name, -1);
        String hint = ta.getString(R.styleable.InputLayout_hint);
        int hintRes = ta.getResourceId(R.styleable.InputLayout_hint, -1);
        String textRight = ta.getString(R.styleable.InputLayout_text_right);
        int textRightRes = ta.getResourceId(R.styleable.InputLayout_text_right, -1);
        int imgRight = ta.getResourceId(R.styleable.InputLayout_img_right, -1);
        int imgLeft = ta.getResourceId(R.styleable.InputLayout_img_left, -1);
        int nameColor = ta.getResourceId(R.styleable.InputLayout_name_color, -1);
        boolean isRequired = ta.getBoolean(R.styleable.InputLayout_is_required, false);
        isEdit = ta.getBoolean(R.styleable.InputLayout_is_input, true);
        initView();
        setName(name);
        setName(nameRes);
        setHint(hint);
        setHint(hintRes);
        setTextRight(textRight);
        setTextRight(textRightRes);
        isRequired(isRequired);
        setRightImg(imgRight);
        setLeftImg(imgLeft);
        setNameColor(nameColor);
        isEdite(isEdit);
        ta.recycle();

    }

    private void initView() {
        tvName = (TextView) findViewById(R.id.tv_name);
        tvRight = (TextView) findViewById(R.id.tv_right);
        tvCenter = (TextView) findViewById(R.id.tv_center);
        ivRight = (ImageView) findViewById(R.id.iv_right);
        ivLeft = (ImageView) findViewById(R.id.iv_left);
        tvRequired = (TextView) findViewById(R.id.tv_required);
        clearEditView = (EditText) findViewById(R.id.et_text);
        llInput = findViewById(R.id.ll_input);
    }

    public void setNameColor(int color) {
        if (color != -1) {
            tvName.setTextColor(getResources().getColor(color));
        }
    }

    public void setName(String name) {
        if (StringUtils.isNotBlank(name)) {
            tvName.setText(name);
        }
    }

    public void setName(int nameRes) {
        if (nameRes != -1) {
            tvName.setText(nameRes);
        }
    }

    public void setRightImg(int res) {
        if (res != -1) {
            ivRight.setVisibility(VISIBLE);
            ivRight.setImageResource(res);
        } else {
            ivRight.setVisibility(GONE);
        }
    }

    public void setLeftImg(int res) {
        if (res != -1) {
            ivLeft.setVisibility(VISIBLE);
            ivLeft.setImageResource(res);
        } else {
            ivLeft.setVisibility(GONE);
        }
    }

    public void setTextRight(String text) {
        if (StringUtils.isNotBlank(text)) {
            tvRight.setVisibility(VISIBLE);
            tvRight.setText(text);
        }
    }

    public void setTextRight(int text) {
        if (text != -1) {
            tvRight.setVisibility(VISIBLE);
            tvRight.setText(text);
        }
    }

    public void setHint(String hint) {
        if (StringUtils.isNotBlank(hint)) {
            if (isEdit) {
                clearEditView.setHint(hint);
            } else {
                tvCenter.setHint(hint);
            }
        }
    }

    public void setTextCenter(String textCenter) {
        if (StringUtils.isNotBlank(textCenter)) {
            tvCenter.setVisibility(VISIBLE);
            tvCenter.setText(textCenter);
        }
    }

    public void setText(String text) {
        if (isEdit) {
            clearEditView.setText(text);
        } else {
            setTextCenter(text);
        }
    }

    public void setHint(int hintRes) {

        if (hintRes != -1) {
            if (isEdit) {
                clearEditView.setHint(hintRes);
            } else {
                tvCenter.setHint(hintRes);
            }
        }
    }

    public void isRequired(boolean isRequired) {
        if (isRequired) {
            tvRequired.setVisibility(VISIBLE);
        } else {
            tvRequired.setVisibility(GONE);
        }
    }

    public String getText() {
        if (isEdit) {
            return clearEditView.getText().toString();
        } else {
            return tvCenter.getText().toString();
        }
    }

    public void setFouceEnd() {
        clearEditView.requestFocus();
        clearEditView.setSelection(getText().length());

    }

    public TextView getTvCenter() {
        return tvCenter;
    }

    public EditText getEditView() {
        return clearEditView;
    }

    public void isEdite(boolean isEdit) {
        if (isEdit) {
            clearEditView.setVisibility(VISIBLE);
            tvCenter.setVisibility(GONE);
        } else {
            clearEditView.setVisibility(GONE);
            tvCenter.setVisibility(VISIBLE);
        }
    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        super.setOnClickListener(l);
        llInput.setOnClickListener(l);
    }
}
